import { ElMessage } from "element-plus"
import VueRouter from '@/router/index'

// 退出登录
export const logOutLogin = () => {
  const router = VueRouter
  removeSession(['token', 'userinfo'])
  ElMessage({type: 'success', message: '退出登录成功', duration: 800})
  setTimeout(() => {
    router.push('/login')
  }, 500);
}

// 保存session
export const saveSession = (obj: any) => {
  if(JSON.stringify(obj) !== '{}' && Object.prototype.toString.call(obj) === '[object Object]') {
    for(let key in obj) {
      sessionStorage.setItem(key, obj[key]);
    }
  }
}

// 清除session
export const removeSession = (arr: any[]) => {
  if(arr.length) {
    arr.forEach(item => {
      sessionStorage.removeItem(item);
    })
  }
}

// 获取session
export const getSession = (arr: any[]) => {
  let obj: any = {}
  if(arr.length) {
    arr.forEach(item => {
      const data = sessionStorage.getItem(item) || ''
      obj[item] = (data?.indexOf('{') !== -1 && data?.indexOf('}') !== -1) ? JSON.parse(data) : data
    })
  }
  return obj
}